package samples.chapter04;

import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/chapter04")
@RestController
public class Chapter04BookController {

    @PostMapping("/")
    @CrossOrigin(value = "http://localhost:8084", maxAge = 1800, allowedHeaders = "*")
    public String addBook(String name) {
        return "receive: " + name;
    }

    @DeleteMapping("/{id}")
    @CrossOrigin(value = "http://localhost:8084", maxAge = 1800, allowedHeaders = "*")
    public String deleteBook(@PathVariable Long id) {
        return String.valueOf(id);
    }

    @GetMapping("/book")
    public Chapter04Book book() {
        Chapter04Book book = new Chapter04Book();
        book.setName("罗贯中");
        book.setAuthor("罗贯中");
        book.setPrice(30f);
        book.setPublicationDate(new Date());
        return book;
    }

    @GetMapping("/book2")
    public String book2(@ModelAttribute("b") Chapter04Book book, @ModelAttribute("a") Chapter04Author author) {
        return book.toString() + ": " + author.toString();
    }
}
